// 工具箱
import ToolsFactory from './toolsFactory'

// 根据数据绘制转孔信息
export default class DrawProfile {
    constructor() {
        this.length = 0;
        this.index = 0;
        this.dataList = [];
        this.toolsList = new ToolsFactory()
        this.DataSource = null;
        this.ToolsFactory = new ToolsFactory()
    }
    delDataSource() {
        this.DataSource && viewer.dataSources.remove(this.DataSource);
    }
    init(data) {
        this.index = 0;
        this.dataList = [];
        this.dataList = data;
        // viewer.scene.globe.depthTestAgainstTerrain = false;
        // viewer.scene.fxaa = false
        // viewer.scene.postProcessStages.fxaa.enabled = false;
        // var supportsImageRenderingPixelated = viewer.cesiumWidget._supportsImageRenderingPixelated;
        // if (supportsImageRenderingPixelated) {
        //     var vtxf_dpr = window.devicePixelRatio;
        //     while (vtxf_dpr >= 2.0) { vtxf_dpr /= 2.0; }
        //     viewer.resolutionScale = vtxf_dpr;
        // }

        this.DataSource = null;
        // viewer.dataSources.removeAll();
        this.DataSource = this.DataSource || new Cesium.CustomDataSource('profileEntities');

        // 数据处理
        // const proList = [];
        // this.dataList.forEach(item => {
        //     proList.push(this.ToolsFactory.getCoordinatesByLonLat(item.coordinates[0], item.coordinates[1]))
        // })
        if (this.dataList.length > 0) {
            this.drawSingleEntity(this.dataList[this.index])
        }
        viewer.dataSources.add(this.DataSource);
        // return this.DataSource
    }
    SourceshowHide(t) {
        if (viewer.dataSources.getByName("profileEntities")[0]) {
            viewer.dataSources.getByName("profileEntities")[0].entities.values.forEach(en => {
                en.show = t
            })
        }
    }
    drawSingleEntity(target) {
        const pos = [];
        const posti = target.coordinates
        const promLis = [];
        for (let i = 0; i < posti.length; i += 2) {
            promLis.push(this.toolsList.getCoordinatesByLonLat(target.coordinates[i], target.coordinates[i + 1]));
            pos.push(Cesium.Cartesian3.fromDegrees(target.coordinates[i], target.coordinates[i + 1]));
        }

        const len = posti.length
        const startPosition = new Cesium.Cartesian3.fromDegrees(posti[0], posti[1])
        const endPosition = new Cesium.Cartesian3.fromDegrees(posti[len - 2], posti[len - 1])
        const distance = Cesium.Cartesian3.distance(startPosition, endPosition)

        // console.log('distance=', distance)
        // let len = posti.length
        // const startPosition = new Cesium.Cartesian3.fromDegrees(posti[0], posti[1])
        // const endPosition = new Cesium.Cartesian3.fromDegrees(posti[len - 2], posti[len - 1])
        // const distance = Cesium.Cartesian3.distance(startPosition, endPosition)

        // console.log('distance=', distance)
        const repeat = parseInt((distance * 10) / 268)
        this.DataSource.entities.add({
            id: 'poumian' + target.eventid,
            name: "poumian" + repeat,
            polyline: {
                positions: pos,
                clampToGround: true,
                width: 4,
                // material: new Cesium.ImageMaterialProperty({
                //     image: require("./img/组 2304.png"), //图片
                //     // color: Cesium.Color.BLUE /*.withAlpha(0.2)*/, //颜色
                //     // transparent: 0.8,//颜色透明度
                //     repeat: new Cesium.Cartesian2(repeat, 1), //s,t方向重复次数
                // })
                material: Cesium.Color.ORANGE,
                // new Cesium.PolylineDashMaterialProperty({
                //     color: Cesium.Color.ORANGE,
                //     gapColor: Cesium.Color.ORANGE.withAlpha(1),
                // }),
            },
            position: Cesium.Cartesian3.fromDegrees(target.coordinates[0], target.coordinates[1], 0),
            label: {
                text: target.name,
                color: Cesium.Color.fromCssColorString('#fff'),
                font: 'normal 14px MicroSoft YaHei',
                showBackground: true,
                // backgroundColor: Cesium.Color.TURQUOISE.withAlpha(0.5),
                outlineColor: new Cesium.Color(0.0, 0.0, 0.0, 1.0),
                outlineWidth: 2.0,
                heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                scale: 1,
                pixelOffset: new Cesium.Cartesian2(0.0, 0.0),
                pixelOffsetScaleByDistance: new Cesium.NearFarScalar(1.5e2, 0.0, 8.0e6, 1.0),//根据距照相机的距离指定广告牌的像素偏移
                distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, 30000.0),
            },
        });

        if (this.index <= this.dataList.length - 1) {
            this.index++;
            this.dataList[this.index] && this.drawSingleEntity(this.dataList[this.index])
        }
        viewer.flyTo(this.DataSource);
    }

    changeProfileStyle(id) {
        let entBox = null;
        const poOBjArrsource = viewer.dataSources.getByName("profileEntities") || [];
        if (poOBjArrsource.length) {
            entBox = poOBjArrsource[0].entities.getById(
                "poumian" + id
            );
            const rep = entBox.name.replace('poumian', '');
            // 设置线的颜色
            // entBox.polyline.material = new Cesium.ImageMaterialProperty({
            //     image: require("./img/组 2286(1).png"), //图片
            //     // color: Cesium.Color.BLUE /*.withAlpha(0.2)*/, //颜色
            //     // transparent: 0.8,//颜色透明度
            //     repeat: new Cesium.Cartesian2(parseInt(rep), 1), //s,t方向重复次数
            // })

            entBox.polyline.material = Cesium.Color.RED;
            // new Cesium.PolylineDashMaterialProperty({
            //     color: Cesium.Color.ORANGERED,
            //     // gapColor: Cesium.Color.ORANGERED.withAlpha(0.7),
            //     // dashLength: 8.0,
            // });
            // entBox  
            const coor = entBox.polyline._positions._value;
            const leg = parseInt(coor.length / 2);
            // viewer.zoomTo(entBox)
            const cartesian3 = coor[leg];
            // var carte = new Cesium.cartesian3(cartesian3.x, cartesian3.y, cartesian3.z);
            var cartographic = viewer.scene.globe.ellipsoid.cartesianToCartographic(cartesian3);

            var lat = Cesium.Math.toDegrees(cartographic.latitude);

            var lng = Cesium.Math.toDegrees(cartographic.longitude);
            var alt = cartographic.height;
            viewer.camera.flyTo({
                destination: Cesium.Cartesian3.fromDegrees(lng, lat, 2800), //摄像机的最终位置
                duration: 1,                           //飞行所用时间
            });
        }

    }
    backOriginalStyle(id) {
        // const poOBjArrsource = viewer.dataSources.getByName("profileEntities") || [];
        let entBox = null;
        const poOBjArrsource = viewer.dataSources.getByName("profileEntities") || [];
        if (poOBjArrsource.length) {
            entBox = poOBjArrsource[0].entities.getById(
                "poumian" + id
            );

            const rep = entBox.name.replace('poumian', '');
            // 设置线的颜色
            // entBox.polyline.material = new Cesium.ImageMaterialProperty({
            //     image: require("./img/组 2304.png"), //图片
            //     // color: Cesium.Color.BLUE /*.withAlpha(0.2)*/, //颜色
            //     // transparent: 0.8,//颜色透明度
            //     repeat: new Cesium.Cartesian2(parseInt(rep), 1),
            // })
            // entBox.polyline.material = new Cesium.PolylineDashMaterialProperty({
            //     color: Cesium.Color.ORANGE,
            //     // gapColor: Cesium.Color.ORANGE.withAlpha(0.4),
            //     // dashLength: 8.0,
            // });
            entBox.polyline.material = Cesium.Color.ORANGE;
        }

    }
    hideSource() {
        const poOBjArrsource = viewer.dataSources.getByName("profileEntities") || [];
        poOBjArrsource.lenght && (poOBjArrsource[0].show = false)
    }
    showSource() {
        const poOBjArrsource = viewer.dataSources.getByName("profileEntities") || [];
        poOBjArrsource.lenght && (poOBjArrsource[0].show = true)
    }
}